package com.lanou.projecta_163news.fragment.newsFragment;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lanou.projecta_163news.base.BaseFragment;
import com.lanou.projecta_163news.adapter.newsAdapter.News;
import com.lanou.projecta_163news.adapter.newsAdapter.NewsAdapter;
import com.lanou.projecta_163news.adapter.newsAdapter.NewsItemListener;
import com.lanou.projecta_163news.adapter.newsAdapter.NewsRecyclerAdapter;
import com.lanou.projecta_163news.fragment.newsViewPager.NewsFourFrament;
import com.lanou.projecta_163news.fragment.newsViewPager.NewsOneFrament;
import com.lanou.projecta_163news.fragment.newsViewPager.NewsThreeFrament;
import com.lanou.projecta_163news.fragment.newsViewPager.NewsTwoFrament;
import com.lanou.projecta_163news.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 16/3/5.
 */
public class NewsFragment extends BaseFragment implements NewsItemListener {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ArrayList<Fragment> neFrData;
    private NewsAdapter newsAdapter;
    private RecyclerView recyclerView;
    private NewsRecyclerAdapter newsRecyclerAdapter;
    private List<News> newsData;

    @Override
    protected void initView() {
        viewPager = bindView(R.id.news_viewpager);
        tabLayout = bindView(R.id.news_tablayout);
        recyclerView = bindView(R.id.news_recyclerview);


    }

    @Override
    protected void initData() {
        ininView();
        ininRecyclerView();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_news;
    }

    @Override
    protected ViewGroup getNull() {
        return null;
    }

    private void ininView() {
        neFrData = new ArrayList<>();
        neFrData.add(new NewsOneFrament());
        neFrData.add(new NewsTwoFrament());
        neFrData.add(new NewsThreeFrament());
        neFrData.add(new NewsFourFrament());
        newsAdapter = new NewsAdapter(getChildFragmentManager(), neFrData, getContext());
        viewPager.setAdapter(newsAdapter);
        tabLayout.setSelectedTabIndicatorColor(Color.alpha(0));
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(newsAdapter.getTabView(i));
            }
        }
        viewPager.setCurrentItem(2);
        viewPager.setCurrentItem(0);
    }

    private void ininRecyclerView() {
        newsRecyclerAdapter = new NewsRecyclerAdapter();
        recyclerView.setAdapter(newsRecyclerAdapter);
        GridLayoutManager gm = new GridLayoutManager(getContext(), 1);
        gm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gm);

        newsData = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            newsData.add(new News("hahahahaha", "哈哈哈哈哈哈啊哈哈哈哈哈哈", R.mipmap.ic_launcher));
            newsData.add(new News("trttttttt", "天天天天天天天天天天", R.mipmap.ic_launcher));
            newsData.add(new News("uuuuuuuuuuuuuuuu", "开开开开开开开开开开开开咖啡馆和规范和方法的话就会感觉", R.mipmap.ic_launcher));
            newsData.add(new News("奋斗过三个", "是对方告诉他的一年级孙婷一人吗么一人拿上来发的是更好地发挥", R.mipmap.ic_launcher));
            newsData.add(new News("是东风公司的非官方的三个", "收到更好的身上的还是商业软件看目录阿萨德开飞机可能就打开方式就卡死", R.mipmap.ic_launcher));
            newsData.add(new News("都烦死个梵蒂冈", "啊胡歌为吼吼为火锅鸡肉为进来查看女女1爱来过挤房间给我哦积分上东国际破碎的房间看公屏看看好了", R.mipmap.ic_launcher));
            newsData.add(new News("双方的公司分公司的感受对方2", "sdf两个户口破壳我不要亏本网页坡博1111大法官巴特发帖本", R.mipmap.ic_launcher));
            newsData.add(new News("是电饭锅电饭锅都是该死的2", "阿迪达斯法法规和低收入一句要离开奋斗故事是太湖经济应该很快谬以", R.mipmap.ic_launcher));
            newsData.add(new News("双方的公司的法国大使馆", "破天荒扣扣上太空好浦科特客户扑克好痛苦活佛为急哦判断和平均可突破五格局看姑婆儿科", R.mipmap.ic_launcher));
            newsData.add(new News("sdf郭德纲和发送的感受对方", "id方式价格和JPG空间大富婆我根据客户家坡头是健康活泼我开工hi恐怕时刻", R.mipmap.ic_launcher));
        }
        newsRecyclerAdapter.addNewsData(newsData);
        newsRecyclerAdapter.setNewsItemListener(this);
    }

    @Override
    public void newsItemListener(News news) {
        Toast.makeText(getContext(), news.getNewsTitles().toString(), Toast.LENGTH_SHORT).show();
    }
}
